export default function useMinWaitPromise<T>(
  last: number,
  callback?: (() => T) | (() => Promise<T>) | T,
  wait = 500
): Promise<T | void> {
  return new Promise((resolve) => {
    // setTimeout(
    //   () => {
    //     const result = callback instanceof Function ? callback() : callback
    //     resolve(result)
    //   },
    //   gap < wait ? wait - gap : 0
    // )
    Promise.resolve(callback instanceof Function ? callback() : callback).then((result) => {
      const gap = Date.now() - last
      const waitTime = gap < wait ? wait - gap : 0
      useDelay<T>(waitTime, result).then(resolve)
    })
  })
}

export function useDelay<T>(wait = 350, value?: T): Promise<T | void> {
  return new Promise((resolve) => {
    setTimeout(() => resolve(value), wait)
  })
}
